package com.space.config;

import com.space.entity.UiConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Component
public class ConfigurationJdbc {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    private static final ConfigurationRowMapper ConfigurationRowMapper = new ConfigurationRowMapper();
    private static class ConfigurationRowMapper implements RowMapper<UiConfiguration> {
        @Override
        public UiConfiguration mapRow(ResultSet rs, int rowNum) throws SQLException {
            UiConfiguration user = new UiConfiguration();
            user.setId(rs.getString("id"));
            user.setData(rs.getString("data"));
            return user;
        }
    }
    // 插入数据  dm不输入id添加失败
    public int insert(String data, String id) {
        String sql = "INSERT INTO ui_configuration (data, id) VALUES (?, ?)";
        return jdbcTemplate.update(sql, data, id);
    }



    // 查询数据 需要判断返回的集合是否为空
    public List<UiConfiguration> getById(String id) {
        String sql = "SELECT * FROM ui_configuration WHERE id = ?";
        return jdbcTemplate.query(sql, new Object[]{id}, ConfigurationRowMapper);
    }

    public List<UiConfiguration> get() {
        String sql = "SELECT * FROM ui_configuration ";
        return jdbcTemplate.query(sql, ConfigurationRowMapper);
    }

    // 更新数据
    public int update(String id, String name) {
        String sql = "UPDATE ui_configuration SET data = ? WHERE id = ?";
        return jdbcTemplate.update(sql, name, id);
    }

    // 删除数据
    public int delete(String id) {
        String sql = "DELETE FROM ui_configuration WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }

    public int deleteAll() {
        String sql = "DELETE FROM ui_configuration";
        return jdbcTemplate.update(sql);
    }

    public int create(String sql) {
        return jdbcTemplate.update(sql);
    }

    public List<Map<String, Object>> excute(String sql) {
        return jdbcTemplate.queryForList(sql);
    }
}
